
#include "tlv12.h"

bool IsLittleEndian()
{
	int value = 0x04030201;
	char *p = (char*)(&value);
	return *p == 1;
}

uint16_t my_htons(uint16_t a)  {	return IsLittleEndian() ? _htons(a) : a; }
uint16_t my_ntohs(uint16_t a)  {	return IsLittleEndian() ? _ntohs(a) : a; }
uint32_t my_htonl(uint32_t a)  {	return IsLittleEndian() ? _htonl(a) : a; }
uint32_t my_ntohl(uint32_t a)  {	return IsLittleEndian() ? _ntohl(a) : a; }
uint32_t hi_uint32(uint64_t a) {    return (uint32_t)((a >> 32) & 0xffffffff);	}
uint32_t lo_uint32(uint64_t a) {    return (uint32_t)(a & 0xffffffff);			}

static uint64_t _htonl64(uint64_t a)
{
	uint32_t low = lo_uint32(a);
	uint32_t high = hi_uint32(a);
	low = my_htonl(low);
	high = my_htonl(high);
	uint64_t ret = low;
	ret <<= 32;
	ret |= high;
	return ret;
}

static uint64_t _ntohl64(uint64_t a)
{
	uint32_t low = lo_uint32(a);
	uint32_t high = hi_uint32(a);
	low	= my_ntohl(low);
	high = my_ntohl(high);
	uint64_t ret = low;
	ret <<= 32;
	ret |= high;
	return ret;
}

uint64_t my_htonl64(uint64_t a){	return IsLittleEndian() ? _htonl64(a) : a; }
uint64_t my_ntohl64(uint64_t a){	return IsLittleEndian() ? _ntohl64(a) : a; }

